<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017-07-03
 * Time: 15:37
 */

namespace App\Http\Controllers\Index;



use App\Base\BaseFunc;
use App\Base\OrderStatus;
use App\Http\Controllers\Controller;
use App\Model\Accessory;
use App\Model\Service;
use App\Model\ServiceNum;
use App\Model\Shop;
use App\Model\ShopNum;
use App\Model\Task;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    public function login()
    {
        return view('/index.login');
    }

    public function _login(BaseFunc $baseFunc)
    {
        $input = Input::only('user_phone');
        $password = Input::only('user_password');
        $input['user_password'] = md5($password['user_password']);
        $user = DB::table('user')
            ->where('user_phone',"=",$input['user_phone'])
            ->value('user_password');
        if($user == NULL)
        {
            $baseFunc ->setRedirectMessage(false,"您还没有注册，请先注册",NULL,"/register");
        }
        else
        {
            $userData = DB::table('user')
                ->where('user_phone',"=",$input['user_phone'])
                ->where('user_password',"=",$input['user_password'])
                ->first();
            if($userData == NULL)
            {
                $baseFunc ->setRedirectMessage(false,"密码错误",NULL,"/login");
            }
            else
            {
                session(['user_id' => $userData->user_id]); //获取登录客户的ID
                session(['user_name' => $userData->user_name]); //获取登录客户的名字
                session(['user_phone' => $userData->user_phone]); //获取登录客户的电话

                $baseFunc ->setRedirectMessage(true,"登录成功",NULL,"/");
            }
        }
    }

    public function register()
    {
        return view('/index.register');
    }

    public function _register(BaseFunc $baseFunc)
    {
        $data = Input::only('user_name','user_password','user_phone');
        $password = Input::only('user_password');
        $data['user_password'] = md5($password['user_password']);
        $check = DB::table('user')
            ->where('user_phone',"=",$data['user_phone'])
            ->get();
        if($check == NULL)
        {
            DB::table('user')->insert($data);
            $baseFunc->setRedirectMessage(true,"注册成功，请您登录",NULL,"/login");
        }
        else
        {
            $baseFunc->setRedirectMessage(false,"注册失败，此手机号已被注册",NULL,"/register");
        }
    }

    public function index()
    {
        $data['serviceData'] = Service::join('shop','shop_id','=','service_shop')->get();
        return view('/index.index',$data);
    }

    public function serviceInfo($id)
    {
        $data['serviceData'] = Service::where('service_id',$id)
            ->join('shop','shop_id','=','service_shop')
            ->first();
        return view('/index.serviceInfo',$data);
    }

    public function toTask(Request $request,OrderStatus $orderStatus,BaseFunc $baseFunc)
    {
        $file = $request->file('files');
        $file_name = date('YmdHis').rand(1000,9999).".".$file->getClientOriginalExtension();
        $destinationPath = public_path() . '/files';
        $file->move($destinationPath, $file_name);

        $service = Service::where('service_id',$request->input('task_service'))->first();
        $price = $service->service_price * $request->input('task_num');

        Task::create([
            'task_user' => session('user_id'),
            'task_service' => $request->input('task_service'),
            'task_time' => date('Y-m-d H:i:s'),
            'task_num' => $request->input('task_num'),
            'task_price' => $price,
            'task_info' => $request->input('task_info'),
            'task_status' => $orderStatus->toOrder()->status_id
        ]);
        //插入附件
        $task = Task::where('task_user',session('user_id'))->orderby('task_id','desc')->first();
        Accessory::create([
            'accessory_task' =>  $task->task_id,
            'accessory_address' => $file_name
        ]);
        //服务数量统计
        ServiceNum::create([
           'service_num_user' => session('user_id'),
           'service_num_ids' =>  $request->input('task_service')
        ]);
        $num['service_number'] = ServiceNum::where('service_num_ids', $request->input('task_service'))->count();
        Service::where('service_id', $request->input('task_service'))->update($num);
        //店铺服务总数量统计
        ShopNum::create([
           'shop_num_user' => session('user_id'),
           'shop_num_ids' => $service->service_shop
        ]);
        $shop_num['shop_number'] = ShopNum::where('shop_num_ids',$service->service_shop)->count();
        Shop::where('shop_id',$service->service_shop)->update($shop_num);
        $baseFunc->setRedirectMessage(true,'下单成功,可在我的订单中查看进度',null,null);
        return redirect()->back();
    }

    public function myTask()
    {
        $data['taskData'] = Task::where('task_user',session('user_id'))
            ->join('service','service_id','=','task_service')
            ->join('status','status_id','=','task_status')
            ->join('shop','shop_id','=','service_shop')
            ->get();
        return view('/index.myTask',$data);
    }

    public function toEndTask($id,OrderStatus $orderStatus)
    {
        $status['task_status'] = $orderStatus->end()->status_id;
        Task::where('task_id',$id)->update($status);
        return redirect()->back();
    }

    public function shop($id)
    {
        $data['shopData'] = Shop::where('shop_id',$id)->first();
        $data['serviceData'] = Service::where('service_shop',$id)->get();
        return view('/index.shop',$data);
    }
}